from django.db import models
from django.forms import ModelForm
from django.contrib.auth.models import User
import datetime

#News Post class
class Post(models.Model):
    title = models.CharField(max_length=100)
    body = models.TextField()
    author = models.ForeignKey(User, related_name="newspost_author")
    posted = models.DateTimeField(db_index=True, auto_now_add=True)

    def __unicode__(self):
        return self.title
    
#Returns all comments on the news post    
    def getComments(self):
        return Comment.objects.filter(newspost=self)
        
    class meta:
        ordering = ['-posted']

#On deletion, the delete cascades to all of the comment on the news post        
    def delete(self):
        for comment in self.getComments():
            comment.delete()
        super(Post, self).delete()
    
    
#News Post Comment Class        
class Comment(models.Model):
    body = models.TextField()
    datePosted = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(User, related_name="comment_author")
    dateEdited = models.DateTimeField(null=True, blank=True)
    editor = models.ForeignKey(
                        User, null=True, blank=True, 
                        related_name="editor_editor")
    newspost = models.ForeignKey(Post)

    def __unicode__(self):
        return self.body
    
    class meta:
        ordering = ['datePosted']
        